# -*- coding: utf-8 -*-

from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import HttpResponse


class CrossDomainFormat(MiddlewareMixin):

    def process_request(self, request):
        """解决复杂请求的跨域问题"""
        if request.method == 'OPTIONS':
            return HttpResponse(status=204)

    def process_response(self, request, response):
        # 允许你的域名来获取我的数据
        response['Access-Control-Allow-Origin'] = "*"
        # 允许你携带Content-Type请求头
        response['Access-Control-Allow-Headers'] = "*"
        # 告诉浏览器，缓存预检结果24小时，即针对同一URL请求，发送第一个OPTION请求往后24小时内不再发送OPTION请求。
        response['Access-Control-Max-Age'] = 86400
        # 允许你发送DELETE,PUT
        response['Access-Control-Allow-Methods'] = "DELETE, PUT"
        return response
